package com.studyroom.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.NumberUtil;
import com.studyroom.dto.LoginFormDTO;
import com.studyroom.dto.PwdForm;
import com.studyroom.dto.Result;
import com.studyroom.dto.UserDTO;
import com.studyroom.entity.Icon;
import com.studyroom.entity.User;
import com.studyroom.service.UserService;
import com.studyroom.utils.CacheClient;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

import static com.studyroom.utils.RedisContants.*;
import static com.studyroom.utils.RedisContants.USER_TTL;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2022-11-21
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    private CacheClient cacheClient=new CacheClient ();
    @PostMapping("/login")
    public Result login(@RequestBody LoginFormDTO loginFormDTO){
        Subject subject= SecurityUtils.getSubject ();
        String principal=null;

        AuthenticationToken token=new UsernamePasswordToken (loginFormDTO.getPhone ().length ()==11?loginFormDTO.getPhone ():loginFormDTO.getUsername (),loginFormDTO.getPassword ());
        try {
            subject.login (token);
            //登录成功
            return userService.loginSuccess ();
        }catch (Exception e){
            return Result.fail ("用户名或密码失败");
        }
    }
    @PostMapping("/register")
    public Result register(@RequestBody UserDTO user){
        return userService.register(user);
    }
    @RequestMapping("/logout")
    public Result logout(HttpServletRequest req){
        //登出功能
        return userService.logout(req);
    }
    @RequestMapping("/me")
    public Result me(){
        return userService.me();
    }
    @PostMapping("/updatePwd")
    public Result updatePwd(@RequestBody PwdForm pwdForm){
        return userService.updatePwd(pwdForm);
    }
    @RequestMapping("getIcons")
    public Result getIcons(){
        return userService.getIcons();
    }
    @RequestMapping("updateIcon")
    public Result updateIcon(@RequestBody Icon icon){
        return userService.updateIcon(icon);
    }
    @RequestMapping("getRegisterVerifyCode")
    public Result getRegisterVerifyCode(@RequestParam String phone){
        return userService.getRegisterVerifyCode(phone);
    }
    @RequestMapping("getLoginVerifyCode")
    public Result getLoginVerifyCode(@RequestParam String phone){
        return userService.getLoginVerifyCode(phone);
    }
    @RequestMapping("loginByVerifyCode")
    public Result loginByVerifyCode(@RequestBody UserDTO userDTO){
        return userService.loginByVerifyCode(userDTO);
    }
    @RequestMapping("getOneById")
    public Result getOneById(@RequestParam Long id){
        return userService.getOneById (id);
    }
}
